package pl.olap.sites;

import java.util.ArrayList;

public class Filmweb extends Site implements Parser {

	static final public String URL_MAIN = "http://www.filmweb.pl/search/film?q=&type=&startYear=&endYear=&countryIds=null&genreIds=null&startRate=&endRate=&startCount=&endCount=&sort=COUNT&sortAscending=false&c=portal&page=";
	static final public String URL_SIMPLE = "http://www.filmweb.pl";
	private int countPages = 0;
	private int iterator = 0;
	private ArrayList<String> URLs = new ArrayList<String>();

	public Filmweb() {

	}

	public void setCountPages(String p_content) {
		this.countPages = getCountPagesFromContent(p_content);

	}

	private int getCountPagesFromContent(String p_content) {
		String[] temp = find(p_content,
				"<strong>Wyniki:</strong>.+\\d\\d\\d\\d\\d\\d</div")
				.split("z ");
		String[] res = temp[1].split("</div");
		res[0] = res[0].substring(0, res[0].length() - 2);
		return Integer.parseInt(res[0]);
	}

	public String getNextUrl() {
		iterator = iterator + 1;
		// System.out.println(iterator);
		return this.URL_MAIN + iterator;
	}

	public Integer getCountPages() {
		return this.countPages;
	}

	public ArrayList<String> setURLsMovies(String content) {
		ArrayList<String> result = new ArrayList<String>();
		String[] movies = find(content,
				"<a class=\"hdr hdr-medium\" href=\"/.+</form>").split(
				"dropdownParent well concave pull-right");
		for (int i = 0; i < movies.length - 1; i++) {
			String[] temp = movies[i].split("</a></h3>");
			if (temp.length < 2) {
				System.out.println("nie znalazl adresu filmu");
				continue;
			}
			String[] temp1 = temp[0].split("href=\"");
			if (temp1.length < 2) {
				System.out.println("nie znalazl adresu filmu");
				continue;
			}
			String[] temp2 = temp1[1].split("\">");
			if (temp2.length < 2) {
				System.out.println("nie znalazl adresu filmu");
				continue;
			}
			result.add(temp2[0]);
			URLs.add(temp2[0]);

		}
		return result;
	}

	@Override
	public Integer getNumberFromString(String p_string) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer getYear(String p_content) {
		String[] temp = find(p_content,
				"<span id=filmYear class=halfSize>\\(\\d\\d\\d\\d\\) ").split(
				"\\(");
		if (temp.length < 2) {
			return null;
		}
		String[] result = temp[1].split("\\)");
		if (result.length < 2) {
			return null;
		}
		return Integer.parseInt(result[0]);
	}

	@Override
	public String getTitle(String p_content) {
		String[] temp = find(p_content,
				"a typeof=\"v:Review-aggregate\" href.+property=\"v:name")
				.split("title=");
		if (temp.length < 2) {
			return null;
		}
		String[] title = temp[1].split(" property");
		title[0] = deleteQuote(title[0]);
		return formatString(title[0]);
	}

	@Override
	public String[] getNationality(String p_content) {
		ArrayList<String> result = new ArrayList<String>();
		String[] temp = find(p_content, "produkcja:.+</tr><tr><th>premiera:")
				.split("countryIds=(\\d\\d|\\d|\\d\\d\\d)\">");
		if (temp.length < 2) {
			return null;
		}
		for (int i = 1; i < temp.length; i++) {
			String[] temp1 = temp[i].split("</a>");
			result.add(temp1[0]);
		}
		return result.toArray(new String[result.size()]);
	}

	@Override
	public String getDescription(String p_content) {
		String[] temp = find(p_content,
				"class=filmPlot><p>.+</p></div><div><div class=filmInfo>")
				.split("<p>");
		if (temp.length < 2) {
			return null;
		}
		String[] result = temp[1].split("</p>");
		if (result.length == 2) {
			return formatString(result[0]);
		}
		return null;
	}

	@Override
	public String[] getGenre(String p_content) {
		ArrayList<String> result = new ArrayList<String>();
		String[] temp = find(p_content, "gatunek:.+</tr><tr><th>produkcja:")
				.split("genreIds=(\\d\\d|\\d|\\d\\d\\d)\">");
		if (temp.length < 2) {
			return null;
		}
		for (int i = 1; i < temp.length; i++) {
			String[] temp1 = temp[i].split("</a>");
			result.add(temp1[0]);
		}
		return result.toArray(new String[result.size()]);
	}

	@Override
	public String[] getDirector(String p_content) {
		ArrayList<String> result = new ArrayList<String>();
		String[] temp = find(p_content, "yseria:.+<th>scenariusz").split(
				"title=\"");
		for (int i = 1; i < temp.length; i++) {
			String[] res = temp[i].split("\" rel");
			result.add(res[0]);
		}
		return result.toArray(new String[result.size()]);
	}

	@Override
	public Integer getDuration(String p_content) {
		String[] temp = find(p_content,
				"<i class=icon-small-clock></i>.+</div><div class=communityRate>")
				.split("</i> ");
		if (temp.length < 2) {
			return null;
		}
		String[] temp1 = temp[1].split(" min.</div>");
		if (temp1.length == 1) {
			String[] onlyhours = temp[1].split(" godz.</div>");
			if (onlyhours.length == 2) {
				return Integer.parseInt(deleteWhiteSpaces(onlyhours[0])) * 60;
			}
		}
		if (temp1.length == 2) {
			String[] hours = temp1[0].split("godz\\.");
			int hour = Integer.parseInt(deleteWhiteSpaces(hours[0]));
			int minutes = Integer.parseInt(deleteWhiteSpaces(hours[1]));

			return hour * 60 + minutes;
		}
		return null;
		// TODO Auto-generated method stub
	}

	@Override
	public String[] getCast(String p_content) {
		ArrayList<String> result = new ArrayList<String>();
		String[] cast = find(p_content, "<dt id=role-actors.+role-producer")
				.split("v:starring\">");
		for (int i = 1; i < cast.length; i++) {
			String[] res = cast[i].split("</a>");
			result.add(res[0]);
		}
		return result.toArray(new String[result.size()]);
	}

	@Override
	public Float getRating(String p_content) {
		try {
			String[] result = find(p_content,
					"rel=\"v:rating\" property=\"v:average\"> (\\d\\d|\\d,\\d|\\d)")
					.split("\"> ");
			result[1] = result[1].replaceFirst(",", "\\.");
			return Float.parseFloat(result[1]);
		} catch (Exception e) {
			return Float.parseFloat("0");
		}

	}

	@Override
	public String[] getMoviesContent(String content) {
		String[] movies = find(content,
				"<a class=\"hdr hdr-medium\" href=\"/.+</form>").split(
				"dropdownParent well concave pull-right");
		ArrayList<String> result = new ArrayList<String>();
		for (String movie : movies) {
			String[] temp = movie.split("</a></h3>");
			if (temp.length < 2) {
				continue;
			}
			String[] temp1 = temp[0].split("href=\"");
			if (temp1.length < 2) {
				continue;
			}
			String[] temp2 = temp1[1].split("\">");
			if (temp2.length < 2) {
				continue;
			}
			result.add(temp2[0]);

		}

		return result.toArray(new String[result.size()]);
	}

}
